package com.jzo2o.orders.manager.controller.consumer;

import cn.hutool.core.bean.BeanUtil;
import com.jzo2o.api.market.dto.response.AvailableCouponsResDTO;
import com.jzo2o.api.orders.dto.request.OrderCancelReqDTO;
import com.jzo2o.api.orders.dto.response.OrderResDTO;
import com.jzo2o.api.orders.dto.response.OrderSimpleResDTO;
import com.jzo2o.common.model.CurrentUserInfo;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.mvc.utils.UserContext;
import com.jzo2o.orders.manager.model.dto.OrderCancelDTO;
import com.jzo2o.orders.manager.model.dto.request.OrdersPayReqDTO;
import com.jzo2o.orders.manager.model.dto.request.PlaceOrderReqDTO;
import com.jzo2o.orders.manager.model.dto.response.OrdersPayResDTO;
import com.jzo2o.orders.manager.model.dto.response.PlaceOrderResDTO;
import com.jzo2o.orders.manager.service.IOrdersCreateService;
import com.jzo2o.orders.manager.service.IOrdersManagerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author itcast
 */
@RestController("consumerOrdersController")
@Api(tags = "用户端-订单相关接口")
@RequestMapping("/consumer/orders")
public class ConsumerOrdersController {

    @Resource
    private IOrdersManagerService ordersManagerService;
    @Resource
    private IOrdersCreateService ordersCreateService;


    /**
     * 获取可用优惠卷
     * @param serveId
     * @param purNum
     * @return
     */
    @GetMapping("/getAvailableCoupons")
    @ApiOperation("获取可用优惠券")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "serveId", value = "服务id", required = true, dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "purNum", value = "购买数量，默认1", required = false, dataTypeClass = Long.class)
    })
    public List<AvailableCouponsResDTO> getCoupons(@RequestParam(value = "serveId", required = true) Long serveId,
                                                   @RequestParam(value = "purNum", required = false, defaultValue = "1") Integer purNum) {
        List<AvailableCouponsResDTO> availableCoupons = ordersCreateService.getAvailableCoupons(serveId, purNum);
        return availableCoupons;
    }

    @ApiOperation("下单接口")
    @PostMapping("/place")
    public PlaceOrderResDTO place(@RequestBody PlaceOrderReqDTO placeOrderReqDTO) {
        ordersCreateService.placeOrder(placeOrderReqDTO);
        return null;
    }


    @GetMapping("/{id}")
    @ApiOperation("根据订单id查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "订单id", required = true, dataTypeClass = Long.class)
    })
    public OrderResDTO detail(@PathVariable("id") Long id) {
        return ordersManagerService.getDetail(id);
    }
    
    @GetMapping("/consumerQueryList")
    @ApiOperation("订单滚动分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ordersStatus", value = "订单状态，0：待支付，100：派单中，200：待服务，300：服务中，400：待评价，500：订单完成，600：订单取消，700：已关闭", required = false, dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "sortBy", value = "排序字段", required = false, dataTypeClass = Long.class)
    })
    public List<OrderSimpleResDTO> consumerQueryList(@RequestParam(value = "ordersStatus", required = false) Integer ordersStatus,
                                                     @RequestParam(value = "sortBy", required = false) Long sortBy) {
        return ordersManagerService.consumerQueryList(UserContext.currentUserId(), ordersStatus, sortBy);
    }
    
    /**
     * 对接支付服务的支付接口
     * 这里其实只能算是生成二维码,真正支付,用户就直接对接微信支付接口了,就不用走我们订单管理服务了
     * @param id
     * @param ordersPayReqDTO
     * @return
     */
    @PutMapping("/pay/{id}")
    @ApiOperation("订单支付")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "订单id", required = true, dataTypeClass = Long.class)
    })
    public OrdersPayResDTO pay(@PathVariable("id") Long id, @RequestBody OrdersPayReqDTO ordersPayReqDTO) {

        OrdersPayResDTO ordersPayResDTO = ordersCreateService.pay(id, ordersPayReqDTO);

        return ordersPayResDTO;
    }

    /**
     * 查询订单支付结果
     * 用户通过二维码支付成功后,在小程序点击"支付成功",就能查询订单支付的结果,是否支付成功
     * @param id
     * @return
     */
    @GetMapping("/pay/{id}/result")
    @ApiOperation("查询订单支付结果")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "订单id", required = true, dataTypeClass = Long.class)
    })
    public OrdersPayResDTO payResult(@PathVariable("id") Long id) {
        //根据订单号查询订单信息,,,拿到交易单号
        //根据交易单号请求支付服务,查询对应的支付结果
        //支付成功,,更新订单表
        
        OrdersPayResDTO ordersPayResDTO = ordersCreateService.getPayResultFromTradServer(id);

        return ordersPayResDTO;
    }

    /**
     * 取消订单
     * @param orderCancelReqDTO
     */
    @PutMapping("/cancel")
    @ApiOperation("取消订单")
    public void cancel(@RequestBody OrderCancelReqDTO orderCancelReqDTO) {
        //老师只是为了把请求过来的数据都规范下,所以ordersManagerService里面的cancel方法,
            // 封装成了OrderCancelDTO对象;而没有直接用OrderCancelReqDTO; 老师说其实也可以直接用传进来的参数
            //我自己的理解,,不如方法直接传啥用啥,至于要封装的东西,,统一到service层,进行封装;这样看起来也清楚
        
        //封装OrderCancelDTO对象
        OrderCancelDTO orderCancelDTO = BeanUtils.toBean(orderCancelReqDTO,OrderCancelDTO.class);
        //怪不得,,能在controller层封装,,需要设置的数据,都在线程threadlocal中可以获取到,即通过UserContext
        //用户id
        orderCancelDTO.setCurrentUserId(UserContext.currentUserId());
        //用户名称
        orderCancelDTO.setCurrentUserName(UserContext.currentUser().getName());
        //用户类型
        orderCancelDTO.setCurrentUserType(UserContext.currentUser().getUserType());

        ordersManagerService.cancel(orderCancelDTO);
    }


}
